#include "programfileexchangedialog.h"
#include "ui_programfileexchangedialog.h"
#include <qmessagebox.h>
#include <QDebug>
#include <qfiledialog.h>
#include "filedata.h"
#include "ncparseclass.h"
#include "programtexttransfer.h"
#include <QDir>
#include <QStandardPaths>
#include "nachiparseclass.h"
#include "fanucparseclass.h"


ProgramFileExchangeDialog::ProgramFileExchangeDialog(QWidget *parent) :
    QDialog(parent),
    ui(new Ui::ProgramFileExchangeDialog)
{
    ui->setupUi(this);
    fileData=new FileData;
    ncParseClass=new NcParseClass;
    nachiParse=new NachiParseClass;
    fanucParse=new FanucParseClass;
}

ProgramFileExchangeDialog::~ProgramFileExchangeDialog()
{
    delete ui;
}

void ProgramFileExchangeDialog::on_pushButton_browser_inputProgram_clicked()
{
    //定义文件对话框类
       QFileDialog *fileDialog = new QFileDialog(this);
       fileDialog->resize(400,500);
       //定义文件对话框标题
       fileDialog->setWindowTitle(tr("选择文件"));
       //设置默认文件路径
//       QString tmpFilePath=D_TCRCONTROLLER_DATA_PATH;
//       QString configFile = tmpFilePath+ROBOT_COMMON_NAME+QString::number( robotId )+"/program/";
//       fileDialog->setDirectory(configFile);
       //设置文件过滤器
       fileDialog->setNameFilter(tr("*(*.*)"));
       //设置可以选择多个文件,默认为只能选择一个文件QFileDialog::ExistingFiles
       fileDialog->setFileMode(QFileDialog::ExistingFiles);
       //设置视图模式
       fileDialog->setViewMode(QFileDialog::Detail);
       //打印所有选择的文件的路径
       QStringList fileNames;
       if(fileDialog->exec())
       {
           fileNames = fileDialog->selectedFiles();
       }
        qDebug()<<fileNames;
        if(fileNames.size()>0)
        {
            ui->lineEdit_inFilePath->setText(fileNames[0]);
            on_pushButton_open_file_clicked();
        }
}

void ProgramFileExchangeDialog::on_pushButton_open_file_clicked()
{
    QString inputFilePath=ui->lineEdit_inFilePath->text();


    QFile file(inputFilePath);
    if( !file.exists() )
    {
        QMessageBox::information(NULL, "information", "打开失败２！", QMessageBox::Yes );
        return ;
    }

//    QVector <QString> fileInfoList;//文件每行内容
    ui->textEdit_inputProgram->clear();
    if(file.open(QIODevice::ReadOnly | QIODevice::Text))
    {
        file.reset();
        QTextStream in(&file);
        QString lineStr;
        QString tmpStrInProgram;
        while(!in.atEnd())
        {
            lineStr = in.readLine();
//            ui->textEdit_inputProgram->append(lineStr);
//            fileInfoList.append(lineStr);
            tmpStrInProgram.append(lineStr);
            tmpStrInProgram+="\n";
        }

        ui->textEdit_inputProgram->setPlainText(tmpStrInProgram);
    }
    else
    {
        //消息预留
        //文件打开失败
        QMessageBox::information(NULL, "information", "打开失败１！", QMessageBox::Yes );
        return ;
    }

    file.close();
    file.flush();//必须加，如果不加，可能关机的时候丢失文件内容。
    QMessageBox::information(NULL, "information", "打开成功！", QMessageBox::Yes );

}


void ProgramFileExchangeDialog::on_pushButton_saveOutPutProgramFile_clicked()
{

//    QDir tempDir;
//    if(!tempDir.exists(ui->lineEdit_outFilePath->text()))
//    {
//        qDebug()<<"不存在该路径"<<endl;
//        tempDir.mkpath(ui->lineEdit_outFilePath->text());
//    }


    QString fileName = QFileDialog::getSaveFileName(this, QStringLiteral("保存"),
                                 QStandardPaths::writableLocation(QStandardPaths::DocumentsLocation),
                                                   QStringLiteral( "*(*.*)"));
    if (fileName=="")
    {
        QMessageBox::information(NULL, "information", "未选择文件路径！", QMessageBox::Yes );
        return ;
    }
     QFile file(fileName);
    // 本地文件写入
    file.resize(0);
    if(file.open(QIODevice::ReadWrite | QIODevice::Text))
    {
        QTextStream in(&file);
        in << ui->textEdit_outputProgram->toPlainText()<<"\n";
//        for(int i = 0;i < programList.size();i++)
//        {
//            if(programList[i].contains("\n"))
//            {
//                programList[i].replace("\n","");
//            }
////            qDebug() << "*****"<<__FUNCTION__<<"****"<<__LINE__<<"**programList***"<<programList[i]<<endl;
//            in << programList[i]<<"\n";
//        }
        file.close();
        file.flush();
    }
    else
    {

        file.close();
        QMessageBox::information(NULL, "information", "保存失败！", QMessageBox::Yes );
        return ;
    }

    QMessageBox::information(NULL, "information", "保存成功！", QMessageBox::Yes );
    return ;
}

void ProgramFileExchangeDialog::on_pushButton_doExchange_clicked()
{
    int tmpTool=ui->lineEdit_toolCoordId->text().toInt();
    int tmpUser=ui->lineEdit_userCoorId->text().toInt();
    ncParseClass->setDefaultCoord(tmpTool,tmpUser);
    nachiParse->setDefaultCoord(tmpTool,tmpUser);
    fanucParse->setDefaultCoord(tmpTool,tmpUser);

    QString incomeProgramList;
    //
    incomeProgramList=ui->textEdit_inputProgram->toPlainText();


    QStringList messageOut;
    QVector<PointPro> pointList;
    QStringList cmdStrList;
    int tmpKey;
    switch (ui->comboBox_inputProgramType->currentIndex()) {
    case 0:
        tmpKey=ncParseClass->parseToNulanguage(incomeProgramList,cmdStrList,pointList,messageOut);
        break;
    case 1:
        tmpKey=nachiParse->parseToNulanguage(incomeProgramList,cmdStrList,pointList,messageOut);
        break;
    case 2:
        tmpKey=fanucParse->parseToNulanguage(incomeProgramList,cmdStrList,pointList,messageOut);
        break;
    default:
        break;
    }

    ui->textEdit_message->clear();
    for(int i=0;i<messageOut.size();i++)
    {
        ui->textEdit_message->append(messageOut[i]);
    }


    ProgramTextTransfer textTransfer(0);
    FileInfo fileProInfo;
    QVector<StackInfo> palletStackInfo;

    QMap<int, DPoint> vehicleDPointList;
    QVector<int> saveAxisUnit;
    int globalDefaultUserNum;
    int globalDefaultToolNum;
    QVector<QString> fileInfoListOut;
    textTransfer.writeProgram(fileProInfo,palletStackInfo,pointList,cmdStrList,
                              vehicleDPointList,saveAxisUnit,globalDefaultUserNum,globalDefaultToolNum,
                              fileInfoListOut);

    ui->textEdit_outputProgram->clear();
    QString tmpStrOutProgram;
    for(int i=0;i<fileInfoListOut.size();i++)
    {
//        ui->textEdit_outputProgram->append(fileInfoListOut[i]);
        tmpStrOutProgram.append(fileInfoListOut[i]);
        tmpStrOutProgram+="\n";
    }
    ui->textEdit_outputProgram->setPlainText(tmpStrOutProgram);


    QMessageBox::information(NULL, "information", "转换完成", QMessageBox::Yes );
    return ;
}

void ProgramFileExchangeDialog::on_pushButton_saveOutPutProgramFile_fanuc2g_clicked()
{
    int tmpTool=ui->lineEdit_toolCoordId->text().toInt();
    int tmpUser=ui->lineEdit_userCoorId->text().toInt();
    fanucParse->setDefaultCoord(tmpTool,tmpUser);

    QString incomeProgramList;
    //
    incomeProgramList=ui->lineEdit_inFilePath->text();


    QStringList messageOut;
    QVector<PointPro> pointList;
    QStringList cmdStrList;
    QString tmpStrOutProgram;
    int tmpKey;
    tmpKey=fanucParse->parseToGcode(incomeProgramList,tmpStrOutProgram,messageOut);


    ui->textEdit_message->clear();
    for(int i=0;i<messageOut.size();i++)
    {
        ui->textEdit_message->append(messageOut[i]);
    }




    ui->textEdit_outputProgram->clear();


    ui->textEdit_outputProgram->setPlainText(tmpStrOutProgram);


    QMessageBox::information(NULL, "information", "转换完成", QMessageBox::Yes );
    return ;
}
